<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Userinfo extends MY_Controller {
	public function __construct(){
		parent::__construct();
		$this->sess_checked();
		$this->menuauth();
		$this->load->model('userinfo_m');
	}
	public function lists(){
		$where=array();
        if(isset($_POST['name']) && $_POST['name']!=''){
            $where['name']=array('like',$_POST['name']);
        }
        if(isset($_POST['mobile']) && $_POST['mobile']!=''){
            $where['mobile']=array('like',$_POST['mobile']);
        }
        $res=$this->userinfo_m->ajaxall($where);
        $this->assign('list',$res['list']);
        if(isset($_POST['ajaxs'])){
            $htmlcontent=$this->fetch('userinfo/userinfolists.html');
            $res['list']=$htmlcontent;
            ajax_return(1,'',$res);
        }
        $this->assign('pageshow',$res['pageshow']);
        $this->display('userinfo/index.html');
	}
	public function add(){
		if(isset($_POST['ajaxs'])){//添加数据
            $data=form_ajaxs($_POST['formjquery']);
             if(!cms_length($data['pass'],6)  || !check_regex($data['pass'],'isnozh') ){
            	ajax_return(0,'密码长度不能小于6个英文与数字组成的字符');
        	}
            $this->userinfo_data($data);
            //判断登陆账号是否已经存在
            $where['name']=array('=',$data['name'],'||');
            $where['mobile']=array('=',$data['mobile']);
            if($list=$this->userinfo_m->sel($where)){
            	if($list['name']==$data['name']){
            		 ajax_return(0,'登录名已经存在');
            	}
            	if($list['mobile']==$data['mobile']){
            		ajax_return(0,'手机号已经存在');
            	}
            }
            if($addid=$this->userinfo_m->add($data)){
                $this->adminuserRecord_m->add(1,1,'管理员id为'.$addid);
                ajax_return(1,'添加成功');
            }else{
                $this->adminuserRecord_m->add(1,2,'管理员');
                ajax_return(0,'添加失败');
            }
        }else{
        	$this->display('userinfo/add.html');
        }
	}
	public function edit(){
		if(isset($_POST['ajaxs'])){
			$data=form_ajaxs($_POST['formjquery']);
			$this->userinfo_data($data);
			$where['id']=array('=',$data['id']);
			if(trim($data['pass'])==''){
				unset($data['pass']);
			}else{
				$data['pass']=passcreate($data['pass']);
			}
			  //判断手机账号是否已经存在
            $where_mobile['id']=array('!=',$data['id']);
            $where_mobile['mobile']=array('=',$data['mobile']);
            if($list=$this->userinfo_m->sel($where_mobile)){
            	ajax_return(0,'手机号已经存在');
            }
			if($this->userinfo_m->edit($where,$data)===false){
				ajax_return(1,'提交失败');
			}else{
				ajax_return(0,'提交成功');
			}
		}elseif(isset($_GET['id'])){
		 	$where['id']=array('=',$_GET['id']);
            $list=$this->userinfo_m->sel($where);
            $this->assign('list',$list);
            $this->display('userinfo/edit.html');
		}else{
			 $this->error_msg('参数错误');
		}
	}
	protected function userinfo_data($data){
        if(!cms_length($data['name'],5,16) || !check_regex($data['name'],'isnozh')){
            ajax_return(0,'账号长度为5-15个英文与数字组成的字符');
        }
        if(!check_regex($data['mobile'],'ismobile')){
        	ajax_return(0,'手机号码不匹配');
        }
        if(!check_regex($data['qq'],'number')){
        	ajax_return(0,'qq号码不匹配');
        }
    }
}
